package cn.ikun.dhj.lianxi2;


import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AppCore {
    public static void main(String[] args) throws Exception {
        // TODO
        // 1. 通过反射得到 CoreService 类的实例 c
        // 2. 调用实例 c 的 say 方法并看到输出结果
        // 要求：不要使用 new 关键字
        Class<?> C = Class.forName("cn.ikun.dhj.lianxi2.CoreService");
        Object c = C.newInstance();
        Method say = C.getDeclaredMethod("say");
       Class<?> customerService = Class.forName("cn.ikun.dhj.lianxi2.CustomerService");
//        Object custom = customerService.newInstance();
      Class<?> OrderService = Class.forName("cn.ikun.dhj.lianxi2.OrderService");
//        Object order = OrderService.newInstance();
        Class<?> UserService = Class.forName("cn.ikun.dhj.lianxi2.UserService");
//        Object user = UserService.newInstance();\
        Field[] declaredFields = C.getDeclaredFields();
        for (Field fields: declaredFields){
            Class<?> type = fields.getType();
//            System.out.println(type);
            fields.setAccessible(true);
//            if (type.toString().endsWith("CustomerService")){
//                Object o1 = customerService.newInstance();
//                fields.set(c,o1);
//            }else if (type.toString().endsWith(("OrderService"))){
//                Object o2 = OrderService.newInstance();
//                fields.set(c,o2);
//            }else if (type.toString().endsWith("UserService")){
//                Object o3 = UserService.newInstance();
//                fields.set(c,o3);
//            }
            Object o = type.newInstance();
            fields.set(c,o);
        }
        say.invoke(c);

    }
}
